﻿using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using IT_Tracker.ENTIDADES;
using System.Collections;
using System.Collections.Generic;
using IT_Tracker.DADOS;
using ComponentFactory.Krypton.Toolkit;


namespace IT_Tracker
{
    public partial class Form_TicketALT : KryptonForm
    {
        public Form_TicketALT(string forn, ArrayList cTipo, ArrayList cOperacao, Usuario sUser, Ticket TktAlt)
        {
            InitializeComponent();

            fornecedor = forn;
            comboTipo = cTipo;
            comboOper = cOperacao;
            sessionUser = sUser;
            tkt = TktAlt;
        }

        private void Form_TicketALT_Load(object sender, EventArgs e)
        {
            cboOperacao.DataSource = comboOper;
            cboTipo.DataSource = comboTipo;


            txtTicket.Text = Convert.ToString(tkt.ticket);
            txtChamado.Text = Convert.ToString(tkt.Chamado);
            cboOperacao.Text = tkt.Operacao;
            cboTipo.Text = tkt.Tipo;
            cboArea.Text = tkt.Area;
            cboStatus.Text = tkt.Status;
            txtDescricao.Text = tkt.Descricao;
            rtxtResumo.Text = tkt.Resumo;

            dtpAbertura.Value = tkt.DataAbertura;

            if (Convert.ToString(tkt.DataConclusao) != "01/01/1900 00:00:00")
            {
                mtbEncerramento.Text = Convert.ToString(tkt.DataConclusao);
            }
            if (Convert.ToString(tkt.DataPrevisao) != "01/01/1900 00:00:00")
            {
                mtbPrevisao.Text = Convert.ToString(tkt.DataPrevisao);
            }

        }


        #region Declarações e Variavéis        
        string fornecedor;

        //Entidades/Classes
        Ticket tkt = new Ticket();
        Usuario sessionUser = new Usuario();
        ArrayList comboTipo = new ArrayList();
        ArrayList comboOper = new ArrayList();

        //Metodos BD
        PersistenciaBDTicket bdTkt = new PersistenciaBDTicket();

        #endregion


        #region ******************Validações

        public bool validarTextBox()
        {
            foreach (Control item in this.gboxTicket.Panel.Controls)
            {
                //checa textbox
                if (item.GetType() == typeof(KryptonTextBox))
                {
                    if (item.Text == "")
                    {
                        item.BackColor = Color.Red;

                        return false;
                    }
                    else
                    {
                        item.BackColor = Color.White;
                    }
                }

                //checa combox
                if (item.GetType() == typeof(KryptonComboBox))
                {
                    if (item.Text == "")
                    {
                        item.BackColor = Color.Red;

                        return false;
                    }
                    else
                    {
                        item.BackColor = Color.White;
                    }

                }
            }
            return true;
        }

        private void txtTicket_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+"))
                e.Handled = true;
        }

        private void txtChamado_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+"))
                e.Handled = true;

        }

        #endregion


        #region ***********************************************Eventos Botões TextBox
        private void btnTxtTicket_Click(object sender, EventArgs e)
        {
            txtTicket.Clear();
        }

        private void btnTxtChamado_Click(object sender, EventArgs e)
        {
            txtChamado.Clear();

        }


        private void btntxtOperacaoDir_Click(object sender, EventArgs e)
        {
            if ((cboOperacao.Items.Count - 1) > cboOperacao.SelectedIndex)
            {
                cboOperacao.SelectedIndex++;
            }
        }

        private void btntxtOperacaoEsq_Click(object sender, EventArgs e)
        {
            if ((cboOperacao.SelectedIndex) >= 1)
            {
                cboOperacao.SelectedIndex--;
            }

        }

        private void btntxtAreaEsq_Click(object sender, EventArgs e)
        {
            if ((cboArea.SelectedIndex) >= 1)
            {
                cboArea.SelectedIndex--;
            }

        }

        private void btntxtAreaDir_Click(object sender, EventArgs e)
        {
            if ((cboArea.Items.Count - 1) > cboArea.SelectedIndex)
            {
                cboArea.SelectedIndex++;
            }


        }

        private void btntxtTipoEsq_Click(object sender, EventArgs e)
        {
            if ((cboTipo.SelectedIndex) >= 1)
            {
                cboTipo.SelectedIndex--;
            }

        }

        private void btntxtTipoDir_Click(object sender, EventArgs e)
        {
            if ((cboTipo.Items.Count - 1) > cboTipo.SelectedIndex)
            {
                cboTipo.SelectedIndex++;
            }

        }

        private void btntxtStatusEsq_Click(object sender, EventArgs e)
        {
            if ((cboStatus.SelectedIndex) >= 1)
            {
                cboStatus.SelectedIndex--;
            }

        }

        private void btntxtStatusDir_Click(object sender, EventArgs e)
        {
            if ((cboStatus.Items.Count - 1) > cboStatus.SelectedIndex)
            {
                cboStatus.SelectedIndex++;
            }

        }

        private void btnTxtPrevisao_Click(object sender, EventArgs e)
        {
            mtbPrevisao.Clear();
        }

        private void btnTxtDescricao_Click(object sender, EventArgs e)
        {
            txtDescricao.Clear();
        }

        private void btnTxtResumo_Click(object sender, EventArgs e)
        {
            rtxtResumo.Clear();
        }


        #endregion

        private void btnAlterar_Click(object sender, EventArgs e)
        {
            if ((mtbEncerramento.Text == "  /  /       :") && (cboStatus.Text == "Encerrado") | (cboStatus.Text == "Sem Intervenção"))
            {
                MessageBox.Show("** " + DateTime.Now.ToString() + " ** Favor Inserir a Data de Encerramento do Chamado!");
            }

            else
            {
                if (validarTextBox())
                {

                    Ticket tktv = new Ticket();
                    tktv.ticket = Convert.ToInt32(txtTicket.Text);
                    tktv.Chamado = Convert.ToInt32(txtChamado.Text);
                    tktv.Descricao = txtDescricao.Text;
                    tktv.Operacao = cboOperacao.Text;
                    tktv.Status = cboStatus.Text;
                    tktv.Tipo = cboTipo.Text;
                    tktv.Area = cboArea.Text;
                    tktv.DataAbertura = Convert.ToDateTime(dtpAbertura.Value);
                    if (mtbEncerramento.Text != "  /  /       :")
                    {
                        tktv.DataConclusao = Convert.ToDateTime(mtbEncerramento.Text);
                    }
                    else { tktv.DataConclusao = tkt.DataConclusao; }

                    if (mtbPrevisao.Text != "  /  /")
                    {
                        tktv.DataPrevisao = Convert.ToDateTime(mtbPrevisao.Text);
                    }
                    else { tktv.DataPrevisao = tkt.DataPrevisao; }

                    tktv.DataAtualizacao = DateTime.Now;
                    tktv.Usuario = sessionUser.Nome;

                    tktv.Fornecedor = fornecedor;
                    
                    tktv.Resumo = "\r ====Atualizado Por: " + Convert.ToString(sessionUser.Nome) + " as " + DateTime.Now + "==== \r " + rtxtResumo.Text;

                    try
                    {
                        bdTkt.atualizarTicket(tktv, "spTicketAtualiza");
                        Form_Ticket.retorno = ("** " + DateTime.Now.ToString() + " ** Ticket #" + txtTicket.Text + " atualizado com sucesso!");
                        Close();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("** " + DateTime.Now.ToString() + " ** Error -> " + ex.Message);

                    }

                }
                else
                {
                    MessageBox.Show("Todos os campos devem ser preenchidos!");
                }
            }
        }






    }
}
